//C07:Stack2.cpp
// Converting a list to a stack.
#include <iostream>
#include <fstream>
#include <ostream>
#include <stack>
#include <list>
#include <string>
#include <cstddef>
using namespace std;

// Expects a stack:
template<class Stk>
void stackOut(Stk& s, ostream& os = cout)
{
    while (!s.empty())
    {
        os << s.top() << "\n";
        s.pop();
    }
}

class Line
{
    string line;    // Without leading spaces
    size_t lspaces;    // Number of leading spaces
    public:
        Line(string s) : line(s)
        {
            lspaces = line.find_first_not_of(' ');
            if(lspaces == string::npos)
            {
                lspaces = 0;
            }

            line = line.substr(lspaces);
        }

        friend ostream& operator<<(ostream& os, const Line& l)
        {
            for (size_t i = 0; i < l.lspaces; i++)
            {
                os << ' ';
            }

            return os << l.line;
        }
        // Other functions here...
};

int main(void)
{
    ifstream in("Stack2.cpp");
    list<Line> lines;
    // Read file and store lines in the list:
    string s;
    while (getline(in, s))
    {
        lines.push_front(s);
    }

    // Turn the list into a stack for printing:
    stack<Line, list<Line>> stk(lines);
    stackOut(stk);
}